package cn.itcast.tanhua.mytanhuaserver.service.impl;

import cn.itcast.tanhua.mytanhuadubbointerface.api.HuanXinApi;
import cn.itcast.tanhua.mytanhuadubbointerface.api.UserLikeApi;
import cn.itcast.tanhua.mytanhuaserver.service.AddBlacklistService;
import cn.itcast.tanhua.mytanhuaserver.service.BlacklistService;
import cn.itcast.tanhua.pojo.User;
import cn.itcast.tanhua.utils.UserThreadLocal;
import com.alibaba.dubbo.config.annotation.Reference;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 添加黑名单
 * @author 陈英航
 * @since 2021/12/6 16:24
 */
@Service
@Slf4j
public class AddBlacklistServiceImpl implements AddBlacklistService {

    @Autowired
    private BlacklistService blacklistService;

    @Reference
    private UserLikeApi userLikeApi;

    @Reference
    private HuanXinApi huanXinApi;

    /**
     * 添加黑名单
     * @param userId 好友id
     */
    @Override
    public Boolean addBlacklist(Long userId) {

    try {
        User user = UserThreadLocal.get();
        Boolean result = this.blacklistService.addBlacklist(user.getId(), userId);
        //判断是否已经添加入黑名单
        if (result){
            //添加成功，则删除喜欢
            this.userLikeApi.notLikeUser(user.getId(), userId);
            //添加好友到黑名單
            this.huanXinApi.addBlacklist(user.getId(),userId);

            return true;
        }

        }catch (Exception e){
            log.error("添加黑名单失败" ,e);
        }

        return false;
    }

}
